#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCamera>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->widget->resize(480, 360);

    //QCameraInfo info = QCameraInfo::defaultCamera();
    camera = new QCamera();
    AbstractVideoSurface* surface = new AbstractVideoSurface(this);//图像处理类
    camera->setViewfinder(surface);//将camera拍摄到的图片交给AbstractVideoSurface类对象里面的present函数进行处理

    connect(surface,SIGNAL(sndImage(QImage)),this,SLOT(rcvImage(QImage)), Qt::QueuedConnection);
    camera->start();

    connect(camera, &QCamera::statusChanged, this, [&](QCamera::Status status)
    {
        if(status == QCamera::LoadedStatus)
        {
            //获取摄像头支持的分辨率、帧率等参数
            QList<QCameraViewfinderSettings > ViewSets = camera->supportedViewfinderSettings();
            int i = 0;
            qDebug() << "viewfinderResolutions sizes.len = " << ViewSets.length();
            foreach (QCameraViewfinderSettings ViewSet, ViewSets)
            {
                qDebug() << i++
                         << " max rate = "
                         << ViewSet.maximumFrameRate()
                         << "min rate = "<< ViewSet.minimumFrameRate()
                         << "resolution "<<ViewSet.resolution()
                         << "Format="
                         << ViewSet.pixelFormat()
                         << ""<<ViewSet.pixelAspectRatio();
            }

            QList<QSize>  list = camera->supportedViewfinderResolutions();
            qDebug() << "supportedViewfinderResolutions : " << list.size();

            QList<QCamera::FrameRateRange> l = camera->supportedViewfinderFrameRateRanges();
            qDebug() << "supportedViewfinderResolutions : " << l.size();
        }
    });


#if 0
#if 0
    camera = new QCamera();

    QCameraViewfinder *view_finder = new QCameraViewfinder(this);//QCameraViewfinder  QVideoWidget
    view_finder->setFixedSize(480, 360);

    camera_image_capture = new QCameraImageCapture(camera);

    camera_image_capture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    camera->setCaptureMode(QCamera::CaptureStillImage);//
    camera->setViewfinder(view_finder);

    camera->start(); //启动摄像头
    return ;
#endif

#if 1
    camera = new QCamera();

    QCameraViewfinder *view_finder = new QCameraViewfinder();//QCameraViewfinder  QVideoWidget

    camera_image_capture = new QCameraImageCapture(camera);
    capture_button = new QPushButton();
    save_button = new QPushButton();
    exit_button = new QPushButton();
    display_label = new QLabel(this);

    save_button->setFixedHeight(100);
    capture_button->setFixedHeight(100);

    QHBoxLayout *main_layout = new QHBoxLayout();
    QVBoxLayout *v_layout = new QVBoxLayout();

    display_label->setFixedSize(150, 150);
    display_label->setScaledContents(true);

#if 1
    v_layout->addWidget(display_label);
    v_layout->addStretch();
    v_layout->addWidget(capture_button);
    v_layout->addWidget(save_button);
    v_layout->addWidget(exit_button);
#endif
    main_layout->addWidget(view_finder);
    main_layout->addLayout(v_layout);

    connect(capture_button, &QPushButton::clicked, this, &MainWindow::captureImage);
    connect(save_button, &QPushButton::clicked, this, &MainWindow::saveImage);
    connect(exit_button, &QPushButton::clicked, this, &MainWindow::close);
    connect(camera_image_capture, &QCameraImageCapture::imageCaptured, this, &MainWindow::displayImage);


    camera_image_capture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    camera->setCaptureMode(QCamera::CaptureStillImage);//  CaptureVideo
    camera->setViewfinder(view_finder);

#if 1
    QList<QSize> sizes = camera->supportedViewfinderResolutions();
    qDebug() << "viewfinderResolutions sizes.len = " << sizes.length();
    foreach (QSize size, sizes)
    {
        qDebug() << "Resolutions size = " << size;
    }
    QCameraViewfinderSettings set;
    set.setResolution(1920, 1080);//  //600, 480
    camera->setViewfinderSettings(set);
#endif
    camera->start(); //启动摄像头

    ui->widget->setLayout(main_layout);

    connect(camera, &QCamera::statusChanged, this, [&](QCamera::Status status)
    {
#if 0
        if(status == QCamera::LoadedStatus)
        {
            //获取摄像头支持的分辨率、帧率等参数
            QList<QCameraViewfinderSettings > ViewSets = camera->supportedViewfinderSettings();
            int i = 0;
            qDebug() << "viewfinderResolutions sizes.len = " << ViewSets.length();
            foreach (QCameraViewfinderSettings ViewSet, ViewSets)
            {
                qDebug() << i++
                         << " max rate = "
                         << ViewSet.maximumFrameRate()
                         << "min rate = "<< ViewSet.minimumFrameRate()
                         << "resolution "<<ViewSet.resolution()
                         << "Format="
                         << ViewSet.pixelFormat()
                         << ""<<ViewSet.pixelAspectRatio();
            }
            qDebug() << "support CaptureVideo?" << camera->isCaptureModeSupported(QCamera::CaptureVideo);

            bindRecoder(camera);

            //

            QTimer::singleShot(10000, this, [&]{
                qDebug() << "stop";
                //mMediaRecorder->stop();
            });
        }
#endif
    });

    translateLanguage();
#endif
#endif

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::rcvImage(QImage image)
{
    pix = QPixmap::fromImage(image);
    update();
}

void MainWindow::translateLanguage()
{
    //capture_button->setText(tr("capture"));
    //save_button->setText(tr("save"));
    //exit_button->setText(tr("exit"));
}

void MainWindow::displayImage(int , QImage image)
{
    display_label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::captureImage()
{
    qDebug() << "captureImage";
    //截图
    camera_image_capture->capture();
}

void MainWindow::saveImage()
{
    const QPixmap *pixmap = display_label->pixmap();
    if(pixmap)
    {
        pixmap->save("a.jpg");
    }
}

void MainWindow::bindRecoder(QCamera *camera)
{
    mMediaRecorder = new QMediaRecorder(camera,this);
    connect(mMediaRecorder, static_cast<void(QMediaRecorder::*)(QMediaRecorder::Error)>(&QMediaRecorder::error),
        [=](QMediaRecorder::Error error){
        qDebug()<< "**************" << error << " " << mMediaRecorder->errorString();
    });
    connect(mMediaRecorder,&QMediaRecorder::statusChanged,[=](QMediaRecorder::Status status){
        qDebug()<< "*******11*******" << status;
        if(status == QMediaRecorder::LoadedStatus)
            mMediaRecorder->record();
    });

    //设置音频编码格式
    QAudioEncoderSettings audioSettings;
    audioSettings.setCodec("audio/amr");
    audioSettings.setQuality(QMultimedia::HighQuality);
    //mMediaRecorder->setAudioSettings(audioSettings);

    QVideoEncoderSettings settings = mMediaRecorder->videoSettings();

    settings.setResolution(640,480);
    settings.setQuality(QMultimedia::VeryHighQuality);
    //settings.setFrameRate(30.0);
    settings.setCodec("video/x-wmv");

    mMediaRecorder->setVideoSettings(settings);
    mMediaRecorder->setOutputLocation(QUrl(qApp->applicationDirPath() + "/xzp.avi"));

    const QStringList supportedAudioCodecs =  mMediaRecorder->supportedAudioCodecs();
    for (const QString &codecName : supportedAudioCodecs) {
        QString description = mMediaRecorder->audioCodecDescription(codecName);
        qDebug() << "description : " << description;
    }

    const QStringList supportedVideoCodecs = mMediaRecorder->supportedVideoCodecs();
    for (const QString &codecName : supportedVideoCodecs) {
        QString description = mMediaRecorder->videoCodecDescription(codecName);
        qDebug() << "description : " << description;
    }
}

void MainWindow::paintEvent(QPaintEvent *)
{
    if(!pix.isNull())
    {
        QPainter p(this);
        p.drawPixmap(rect(), pix);
    }
}

